import { _decorator, BoxCollider2D, PhysicsSystem2D, tween, Tween, UITransform, Widget } from 'cc';
import { $planet_BaseUI_poof$ } from '../../../frame/component/ui.base';
import $planet_GameDC_poof$ from '../data/game.data.center';
const { ccclass, property } = _decorator;

@ccclass('$planet_GameGameUI_poof$')
export class $planet_GameGameUI_poof$ extends $planet_BaseUI_poof$ {
	@property(UITransform)
	private ui_floor: UITransform = null;

	private va_floor: abd.$planet_cc_poof$.$planet_VecAssist_poof$;

	protected start(){
		this.$planet_init_poof$();
	}

	private $planet_init_poof$(){
		this.va_floor = new abd.$planet_cc_poof$.$planet_VecAssist_poof$(this.ui_floor.node);
		this.ui_floor.node.setPosition(this.va_floor.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_bottom_poof$'));

		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();
		this.node.getComponentsInChildren(Widget).forEach(wgt=>wgt ? wgt.updateAlignment() : void 0);

		this.$planet_adaptUI_poof$();
	}

	private $planet_adaptUI_poof$(){
		let wall = this.node.getChildByName('wall');
		let canvas_size = abd.$planet_size_poof$.$planet_canvasSize_poof$;
		let uitrans = this.node.getComponent(UITransform);
		let ui_wall = wall.getComponent(UITransform);
		['left', 'right', 'top'].forEach(name=>{
			let node = wall.getChildByName('wall-'+name);
			let ui = node.getComponent(UITransform);
			if(['left', 'right'].includes(name)) ui.height = canvas_size.height * 2;
			else ui.width = canvas_size.width + ui.height*2;
			node.getComponent(BoxCollider2D).size = ui.contentSize.clone();
			let va = new abd.$planet_cc_poof$.$planet_VecAssist_poof$(node, this.node);
			if(name=='top'){
				va.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_top_poof$');
				va.$planet_vec_poof$.add3f(0, canvas_size.height * 1, 0);
			}else va.$planet_getPosition_poof$(name=='left' ? '$planet_over_left_poof$' : '$planet_over_right_poof$', '$planet_bottom_poof$');
			node.setPosition(ui_wall.convertToNodeSpaceAR(uitrans.convertToWorldSpaceAR(va.$planet_vec_poof$)));
		});

		this.node.active = false;
	}

	protected $planet_listening_poof$(){
		let list = [];

		Promise.all([
			$planet_GameDC_poof$.$planet_follow_poof$('$planet_trigger_poof$', '$planet_UI_HOME_poof$', (o, show)=>{
				if(show===o) return void 0;
				show ? this.$planet_enter_poof$() : this.$planet_leave_poof$();
			}, false),
		]).then(ls=>list.push(...ls));

		return list;
	}

	/**
	 * 入场动画
	 * @param duration 入场动画持续时间
	 */
	private $planet_enter_poof$(){
		let duration = 0.5;
		let np = new abd.$planet_NudityPromise_poof$<0>();
		this.node.active = true;
		this.scheduleOnce(()=>np.$planet_resolve_poof$(0), duration+0.05);

		Tween.stopAllByTarget();

		tween(this.ui_floor.node)
		.to(duration, {position: this.va_floor.$planet_getPosition_poof$('$planet_center_poof$', '$planet_bottom_poof$')}, {easing: 'quartInOut'})
		.start();

		np.$planet_promise_poof$.then(()=>this.$planet_runGame_poof$());

		return np.$planet_promise_poof$;
	}

	private $planet_leave_poof$(){
		let duration = 0.5;
		let np = new abd.$planet_NudityPromise_poof$<0>();
		this.node.active = true;
		this.scheduleOnce(()=>np.$planet_resolve_poof$(0), duration+0.05);

		Tween.stopAllByTarget();

		tween(this.ui_floor.node)
		.to(duration, {position: this.va_floor.$planet_getPosition_poof$('$planet_center_poof$', '$planet_over_bottom_poof$')}, {easing: 'quartInOut'})
		.start();

		np.$planet_promise_poof$.then(()=>this.node.active = false);

		return np.$planet_promise_poof$;
	}

	private $planet_runGame_poof$(){
		if(Reflect.get(this, 'asdfasdfasdf')) return void 0;
		PhysicsSystem2D.instance.enable = true;
		//PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
		//EPhysics2DDrawFlags.Pair |
		//EPhysics2DDrawFlags.CenterOfMass |
		//EPhysics2DDrawFlags.Joint |
		//EPhysics2DDrawFlags.Shape;

		Reflect.set(this, 'asdfasdfasdf', 'asdfasdfasdf');
		$planet_GameDC_poof$.$planet_trigger_poof$.$planet_LOAD_GAME_poof$ = true;
	}
}

